<?php
namespace app\channel\controller;
use think\Db;

class GameBag extends Base
{
    //CPS一级渠道和联运渠道可以访问
    public function _initialize(){
        parent::_initialize();
        if(session('admin.cps_pid')!=0){
            $this->error('访问错误');
        }
    }

    public function index(){
        if( request()->isAjax() ){
            $page = input('page');
            $limit = input('limit');
            $gameId = input('game_id');
            $id = input('id');

            $bigId = session('admin.cps_id') ? session('admin.cps_id') : session('admin.sdk_id');

            $where['status'] = 1;
            !empty($gameId) && $where['game_id'] = $gameId;
            !empty($id) && $where['id'] = $id;

            if( ! $count=Db::name('ChannelBag')->where($where)->count() ){
                $this->layTable();
            }

            $select = Db::name('ChannelBag')
                ->where($where)
                ->order('add_time desc')
                ->limit(($page-1)*$limit, $limit)
                ->select();

            $data = [];
            $gameIdName = model('manage/Game')->gameIdName();
            $channelBagType = changeValKey('ChannelBagType', 'id', 'type');
            foreach ($select as $k=>$v){
                $whereNum['cps_id'] = $bigId;
                $whereNum['type_id'] = $v['type_id'];
                $giveNum = Db::name('ChannelBagNum')->where($whereNum)->value('give_num');
                if(isset($giveNum)){
                    $v['give_num'] = $giveNum;
                }
                if($v['add_type']==2){
                    $v['give_num'] = 1;
                }

                if($v['give_num'] == 0){
                    continue;
                }

                $status = '未领取';

                if($v['keep_num'] < $v['give_num']){
                    $status = '礼包码不足';
                }

                //验证是否已经领取过该礼包
                $whereDetail['cps_id'] = $bigId;
                $whereDetail['bag_id'] = $v['id'];
                $whereDetail['status'] = 1;
                if( Db::name('ChannelBagDetail')->where($whereDetail)->count() ){
                    $status = '已领取';
                }

                $data[] = [
                    'id' => $v['id'],
                    'add_type' => $v['add_type'],
                    'game_id' => $v['game_id'],
                    'game_name' => $gameIdName[ $v['game_id'] ],
                    'name' => $v['name'],
                    'type_id' => $channelBagType[ $v['type_id'] ],
                    'give_num' => $v['give_num'],
                    'explain' => $v['explain'],
                    'content' => $v['content'],
                    'add_time' => date('Y-m-d H:i:s', $v['add_time']),
                    'status' => $status
                ];
            }
            $this->layTable(0, '游戏礼包列表', $data, $count);
        }else{
            return view('');
        }
    }

    public function get(){
        $bigId = session('admin.cps_id') ? session('admin.cps_id') : session('admin.sdk_id');

        $find = Db::name('ChannelBag')->field('id,game_id,name,type_id,give_num,keep_num,file_name')->where('id', input('id'))->find();

        $whereNum['cps_id'] = $bigId;
        $whereNum['type_id'] = $find['type_id'];
        $giveNum = Db::name('ChannelBagNum')->where($whereNum)->value('give_num');
        if(isset($giveNum)){
            $find['give_num'] = $giveNum;
        }

        //验证库存大于等于可领取数量才可以领取
        if($find['keep_num'] < $find['give_num']){
            $this->error('礼包码数量不足');
        }

        //验证是否领取过该礼包
        $whereDetail['cps_id'] = $bigId;
        $whereDetail['bag_id'] = $find['id'];
        $whereDetail['status'] = 1;
        if( Db::name('ChannelBagDetail')->where($whereDetail)->count() ){
            $this->error('您已经领取过该礼包');
        }

        //裁剪礼包码并领取
        $filePath = ROOT_PATH.'uploads/bag/channel/';
        import('cut.Io');
        $Io = new \Io($filePath);
        $cutRs = $Io->cutFileMulti($find['file_name'], $find['file_name'], $find['give_num']);

        if($cutRs['status']==false) {
            $this->error($cutRs['message']);
        }

        //领取完成之后的操作
        $insert['cps_id'] = $bigId;
        $insert['bag_id'] = $find['id'];
        $insert['give_num'] = $find['give_num'];
        $insert['get_time'] = time();
        $insert['status'] = 1;

        Db::name('ChannelBagDetail')->insert($insert);
        Db::name('ChannelBag')->where('id', $find['id'])->setDec('keep_num', $find['give_num']);
    }
}
